//
//  CreateGroupViewController.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/4/5.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "CreateGroupViewController.h"
#import "AddGroupImageViewController.h"

@interface CreateGroupViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *groupTF; // 小组姓名
@property (nonatomic, assign) CGFloat tempShu;

@end

@implementation CreateGroupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    // navi的修改
    [self changeNavigation];
    // 布局
    [self addMySubview];
}

// navi的修改
- (void)changeNavigation
{
    [self.navigationController.navigationBar setTranslucent:NO];
    self.navigationItem.title = @"填写小组名称";
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftImageView.image = [UIImage imageNamed:@"houtui"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftImageView];
    UITapGestureRecognizer *leftTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lastPage:)];
    [leftImageView addGestureRecognizer:leftTapGes];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"下一步" style:UIBarButtonItemStylePlain target:self action:@selector(nextPage:)];
    rightItem.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = rightItem;
    
}

#pragma mark - 返回上一页面
- (void)lastPage:(UITapGestureRecognizer *)tapGes
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 下一步
- (void)nextPage:(UIBarButtonItem *)rightItem
{
    
    [self.groupTF resignFirstResponder];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.2 animations:^{
            //self.view.transform = CGAffineTransformMakeTranslation(0, offsetY);
            self.view.transform = CGAffineTransformMakeTranslation(0, 0);
        }];
    });
   
  //   self.groupTF.text = @"请填写小组名称 (至少输入两个字符)";
    BOOL isRight = YES; // 默认可以进行下一步
    if (self.groupTF.text.length == 0 || [self.groupTF.text containsString:@" "] || [self.groupTF.text isEqualToString:@"请填写小组名称 (不超过10个字)"] || self.groupTF.text.length > 10) {
        // 说明不符合要求
        isRight = NO;
        [self addAlertOfUnSuit];
    } else {
        NSLog(@"提交的小组名称:%@", self.groupTF.text);
        isRight = YES;
    }
    
    if (isRight == YES) {
        AddGroupImageViewController *addGroupImageV = [[AddGroupImageViewController alloc] init];
        [self.navigationController pushViewController:addGroupImageV animated:YES];
    }
    
}

// 不符合提醒框
- (void)addAlertOfUnSuit
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提醒" message:@"您的小组名称不正确,不能包含空格并且长度不能超过10个字" preferredStyle:UIAlertControllerStyleAlert];
    // 修改标题颜色
    NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"提醒"];
    [title addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];
    [title addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, 2)];
    [alertVC setValue:title forKey:@"attributedTitle"];
    
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"确定");
    }];
    [alertVC addAction:sureAction];
    [self presentViewController:alertVC animated:YES completion:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:YES completion:nil];
        });
    }];
    
}

#pragma mark - 布局
- (void)addMySubview
{
    UIImageView *groupTopImageV = [[UIImageView alloc] initWithFrame:CGRectMake(-1 * kAdjustParamater, - 1 * kAdjustParamater, kScreenWidth + 2 * kAdjustParamater, 370 * kAdjustParamater)];
   // groupTopImageV.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.3];
    groupTopImageV.image = [UIImage imageNamed:@"groupName-tianjia"];
    [self.view addSubview:groupTopImageV];
    
    // 小组姓名
    self.groupTF = [[UITextField alloc] initWithFrame:CGRectMake(17 * kAdjustParamater, groupTopImageV.bottom + 13 * kAdjustParamater, 300 * kAdjustParamater, 20 * kAdjustParamater)];
    self.groupTF.text = @"请填写小组名称 (不超过10个字)";
    self.groupTF.textColor = kCyanColor;
    self.groupTF.textAlignment = NSTextAlignmentLeft;
    self.groupTF.delegate = self;
   // self.groupTF.clearsOnBeginEditing = YES;
    self.groupTF.tintColor = kCyanColor;
    self.groupTF.font = [UIFont systemFontOfSize:k14Big];
    [self.groupTF addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventAllEditingEvents];
    [self.view addSubview:_groupTF];

    
    // 小组名字下方的浅色条
    UILabel *cyanLine = [[UILabel alloc] initWithFrame:CGRectMake(0, self.groupTF.bottom + 13 * kAdjustParamater, kScreenWidth, 1 * kAdjustParamater)];
    cyanLine.backgroundColor = kCyanColor;
    [self.view addSubview:cyanLine];
}

- (void)valueChange:(UITextField *)tempText
{
    NSLog(@"textField = %@", tempText.text);
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    [UIView animateWithDuration:0.2 animations:^{
      
        self.view.transform = CGAffineTransformMakeTranslation(0, 0);
    }];
    if (textField.text.length == 0 || [textField.text containsString:@" "]) {
        textField.text = @"请填写小组名称 (不超过10个字)";
    }
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if ([textField.text isEqualToString:@"请填写小组名称 (不超过10个字)"] || [textField.text containsString:@" "]) {
        textField.text = @"";
    }
    
    [UIView animateWithDuration:0.2 animations:^{
       
        self.view.transform = CGAffineTransformMakeTranslation(0, -94 * kAdjustParamater);
    }];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
